package com.atguigu.gmall.order.config;

import com.atguigu.gmall.mq.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lfy
 * @Description  订单服务依赖的所有 交换机、队列提前创建好
 * @create 2023-08-04 11:25
 */
@Configuration
public class AppMqConfiguration {

    //规则

    /**
     * 订单事件交换机： 接受订单的所有消息
     * @return
     */
    @Bean
    Exchange orderEventExchange(){
        /**
         * String name,  交换机名
         * boolean durable,  持久化
         * boolean autoDelete 自动删除
         */
       return new TopicExchange(MqConst.EXCHANGE_ORDER,true,false);
    }


    /**
     * 订单延迟队列
     * @return
     */
    @Bean
    Queue orderDelayQueue(){

        /**
         * String name,  队列名
         * boolean durable, 持久化
         * boolean exclusive, 排他
         * boolean autoDelete, 自动删除
         * Map<String, Object> arguments 参数
         */
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange",MqConst.EXCHANGE_ORDER);
        arguments.put("x-dead-letter-routing-key",MqConst.RK_ORDER_TIMEOUT);
        arguments.put("x-message-ttl",MqConst.ORDER_TTL);
        return new Queue(MqConst.QUEUE_ORDER_DELAY,true,false,false,arguments);
    }

    @Bean
    Queue orderDeadQueue(){
        return new Queue(MqConst.QUEUE_ORDER_DEAD,true,false,false);
    }


    /**
     * 支付成功单队列
     * @return
     */
    @Bean
    Queue orderPayedQueue(){
        return new Queue(MqConst.QUEUE_ORDER_PAYED,true,false,false);
    }

    //===========声明绑定关系======
    @Bean
    Binding orderPayedQueueBinding(){
        return new Binding(
                MqConst.QUEUE_ORDER_PAYED,
                Binding.DestinationType.QUEUE,
                MqConst.EXCHANGE_ORDER,
                MqConst.RK_ORDER_PAYED,
                null
        );
    }

    /**
     * 延迟队列绑定
     * @return
     */
    @Bean
    Binding orderDelayQueueBinding(){
        /**
         * String destination, 目的地
         * DestinationType destinationType,  目的地类型
         * String exchange, 交换机
         * String routingKey, 路由键
         * @Nullable Map<String, Object> arguments 参数
         */
        return new Binding(
                MqConst.QUEUE_ORDER_DELAY,
                Binding.DestinationType.QUEUE,
                MqConst.EXCHANGE_ORDER,
                MqConst.RK_ORDER_CREATE,
                null
                );
    }

    /**
     * 死信路由绑定
     * @return
     */
    @Bean
    Binding orderDeadQueueBinding(){
        return new Binding(
                MqConst.QUEUE_ORDER_DEAD,
                Binding.DestinationType.QUEUE,
                MqConst.EXCHANGE_ORDER,
                MqConst.RK_ORDER_TIMEOUT,
                null
        );
    }

}
